package com.lp;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Collections;
import java.util.function.Function;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-03-10 21:01:54
 */
public class MybatisPlusGenerator {

    private static final String PROJECT_PATH = System.getProperty("user.dir") + "/temp";

    private static final String BASE = "com.lp.biz";

    private static final String URL
            = "jdbc:mysql://47.108.58.110:3306/lp?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";

    private static final String USER = "leap-planet";

    private static final String PW = "hwDjX3iWwDn72Daa";

    /**
     * main functio
     * n entrance
     * @param args default parameters
     */
    public static void main(String[] args) {
        FastAutoGenerator.create(initDataSourceConfig())
                .globalConfig(MybatisPlusGenerator::initGlobalConfig)
                .packageConfig(MybatisPlusGenerator::initPackageConfig)
                .strategyConfig(MybatisPlusGenerator::initStrategyConfig)
                .injectionConfig(MybatisPlusGenerator::initInjectionConfig)
                .templateConfig(MybatisPlusGenerator::initTemplateConfig)
                .execute();
    }

    /**
     * 初始化数据源
     */
    private static DataSourceConfig.Builder initDataSourceConfig() {
        return new DataSourceConfig.Builder(URL, USER, PW)
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler());
    }

    /**
     * 初始化包配置
     */
    private static void initPackageConfig(Function<String, String> scanner, PackageConfig.Builder builder) {
        String moduleName = scanner.apply("请输入模块名:");
        builder.parent(BASE)
                .moduleName(moduleName)
                .entity("model.entity")
//                .service("service")
//                .serviceImpl("service.impl")
//                .mapper("mapper")
//                .xml("mapper.xml")
//                .controller("controller")
//                .other("other")
                .pathInfo(Collections.singletonMap(OutputFile.xml, PROJECT_PATH + "/src/main/resources/mapper/" + moduleName));
    }

    /**
     * 初始化策略配置
     */
    private static void initStrategyConfig(Function<String, String> scanner, StrategyConfig.Builder builder) {
        builder.addInclude(scanner.apply("请输入表名，多个英文逗号分割:"))
                .serviceBuilder()
                .formatServiceFileName("%sService")
                .entityBuilder()
                .enableLombok()
                .mapperBuilder()
                .enableBaseResultMap().enableBaseColumnList()
                .controllerBuilder()
                .enableRestStyle();
    }

    /**
     * 初始化公共配置
     */
    private static void initGlobalConfig(Function<String, String> scanner, GlobalConfig.Builder builder) {
        // 作者
        builder.author(scanner.apply("请输入作者名:"))
//                .enableSwagger()
                // 指定输出目录
                .outputDir(PROJECT_PATH + "/src/main/java")
                // 文件头时间
                .commentDate("yyyy-MM-dd HH:mm:ss")
                // 是否打开文件
                .disableOpenDir();
    }

    /**
     * 初始化模版
     */
    private static void initTemplateConfig(TemplateConfig.Builder builder) {
        builder.entity("/vm/entity.java")
                .service("/vm/service.java")
                .serviceImpl("/vm/serviceImpl.java")
                .mapper("/vm/mapper.java")
                .xml("/vm/mapper.xml")
                .controller("/vm/controller.java");
    }

    /**
     * 初始化注入配置
     */
    private static void initInjectionConfig(InjectionConfig.Builder builder) {
        // 增加项目名
        builder.customMap(Collections.singletonMap("projectName", "backend"));
    }
}
